package org.dizena.modules.task.action;

import lombok.extern.slf4j.Slf4j;
import org.dizena.utils.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component("DeleteTempFileJob")
public class DeleteTempFileJob
{
    @Value("${upload.temp}")
    private String uploadTempPath;

    public void deleteTempLocalFile()
    {
        //Arrays.stream(Objects.requireNonNull(dir.listFiles())).iterator().forEachRemaining(FileUtil::deleteFile);

        File dir = new File(uploadTempPath);
        File[] files = dir.listFiles();
        assert files != null;
        for (File file : files)
        {
            long fileTime = file.lastModified();
            long nowTime = System.currentTimeMillis();
            if (nowTime - fileTime > 604800000)
            {
                FileUtil.deleteFile(file);
            }
        }
    }

}
